<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class OrderAddress extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_order_address';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-21 14:48
     * @功能说明:添加订单地址
     */
    public function orderAddressAdd($address_id,$order_id,$send_type=2,$type=1,$address_data=[]){

        $address_model = new Address();

        if($send_type==2){

            $address = $address_model->dataInfo(['id'=>$address_id]);

        }else{

            $address = $address_data;

        }

        if(empty($address)){

            return ['code'=>500,'msg'=>'地址未找到'];
        }

        $insert = [

            'uniacid'  => $address['uniacid'],

            'order_id' => $order_id,

            'user_name'=> $address['user_name'],

            'mobile'   => $address['mobile'],

            'province' => !empty($address['province'])?$address['province']:'',

            'city'     => !empty($address['city'])?$address['city']:'',

            'area'     => !empty($address['area'])?$address['city']:'',

            'lng'      => !empty($address['lng'])?$address['lng']:'',

            'lat'      => !empty($address['lat'])?$address['lat']:'',

            'address'  => !empty($address['address'])?$address['address']:'',

            'address_info' => !empty($address['address_info'])?$address['address_info']:'',

            'type'     => $type,

            'send_type'=> $send_type,

            'address_id'=> $address_id,


        ];

        $res = $this->dataAdd($insert);

        if($res!=1){

            return ['code'=>500,'msg'=>'下单失败'];

        }

        return $res;


    }






}